package com.xmy.cultivate.mapper;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Constants;
import com.xmy.cultivate.entity.Grade;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.*;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 班级 Mapper 接口
 * </p>
 *
 * @author min
 * @since 2022-07-26
 */
public interface GradeMapper extends BaseMapper<Grade> {

    @Select("SELECT * FROM grade")
    @Results(
            {@Result(
                    column = "id",property = "id"
            ),
            @Result(
                    property = "coursePricingList"
                    ,column = "id",
                    one = @One(select = "com.xmy.cultivate.mapper")
            )
    })
    public abstract List<Map<String,Object>> getGradeList(Map<String,Object> map);

    @Select("select a.* from grade as a left join course as b on a.course_id = b.id ${ew.customSqlSegment}")
    @Results({
            @Result( column = "id",property = "id"),
            @Result(
                    property = "courseObj"
                    ,column = "course_id",
                    one=@One(select = "com.xmy.cultivate.mapper.CourseMapper.checkCourseForId")
            ),
            @Result(property = "teacherObj"
                    ,column = "teacher_id",
                    one=@One(select = "com.xmy.cultivate.mapper.StaffMapper.selectById")
            )
    })
    public abstract IPage<Grade> checkGrade(IPage<?> page, @Param(Constants.WRAPPER) Wrapper wrapper);


    @Select("select a.id as studentGradeId,b.id as gradeId,b.`name` as gradeName,b.course_id,getCourseNameForId(b.course_id),b.school_id,getSchoolName(b.school_id) as schoolName from student_grade as a left join grade as b on  a.student_id = b.id where a.status =1 and a.student_id= #{studentId}")
    public abstract List<Map<String,Object>> GetLessonByStuInfoId(Long studentId);


    @Select("SELECT * from grade_view ${ew.customSqlSegment}")
    @Results({
            @Result( column = "id",property = "id"),
            @Result(
                    property = "courseObj"
                    ,column = "course_id",
                    one=@One(select = "com.xmy.cultivate.mapper.CourseMapper.checkCourseForId")
            ),
            @Result(property = "teacherObj"
                    ,column = "teacher_id",
                    one=@One(select = "com.xmy.cultivate.mapper.StaffMapper.selectById")
            ),
            @Result(property = "schoolObj"
                    ,column = "school_id",
                    one=@One(select = "com.xmy.cultivate.mapper.OrganizationMapper.selectById")
            )
    })
    public abstract IPage<Grade> findAll(IPage<?> page, @Param(Constants.WRAPPER) Wrapper wrapper);


    @Select("select a.* from grade as a where id = #{id}")
    @Results({
            @Result( column = "id",property = "id"),
            @Result(
                    property = "courseObj"
                    ,column = "course_id",
                    one=@One(select = "com.xmy.cultivate.mapper.CourseMapper.checkCourseForId")
            ),
            @Result(property = "teacherObj"
                    ,column = "teacher_id",
                    one=@One(select = "com.xmy.cultivate.mapper.StaffMapper.selectById")
            ),
            @Result(property = "schoolObj"
                    ,column = "school_id",
                    one=@One(select = "com.xmy.cultivate.mapper.OrganizationMapper.selectById")
            ),
            @Result(
                    property = "teacherList"
                    ,column = "id",
                    many=@Many(select = "com.xmy.cultivate.mapper.TypeValueDataMapper.selectListByForId")
            )
    })
    public abstract Grade getOneForId(@Param("id") Long id);

}
